package com.kris;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author zhangqunshi
 */
public class DateUtil {

    public final static String DATE_FORMAT_DAY = "yyyy-MM-dd";

    /**
     * 把一个字符串变成日期类型对象
     *
     * @param dateStr
     * @param pattern 日期格式
     * @return
     */
    static Date convert(String dateStr, String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(dateStr);
    }

    /**
     * 只取得日期，去掉时间
     *
     * @param date
     * @param pattern 日期格式
     * @return
     */
    static String onlyGetDate(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    static Date onlyGetDate(Date workDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DAY);
        String date = sdf.format(workDate);
        return sdf.parse(date);
    }

    static Date getCurrentDayFrom10Clock(Date beginTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginTime);
        calendar.set(Calendar.HOUR_OF_DAY, 9);  //早上9点59分59秒
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    static Date getCurrentDayFrom18Clock(Date beginTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginTime);
        calendar.set(Calendar.HOUR_OF_DAY, 18);  //晚上6点
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    static boolean isNotDateType(String dateStr, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            sdf.parse(dateStr);
            return false;
        } catch (ParseException ex) {
            return true;
        }
    }

    static String getTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }

    static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 计算给定的date是否在holiday范围内
     *
     * @param date
     * @param holidays
     * @param excludeHolidays
     * @return
     */
    static boolean isWeekendOrHoliday(Date date, List<Date> holidays,
            List<Date> excludeHolidays) {
        String currday = onlyGetDate(date, DATE_FORMAT_DAY);

        // 如果指定日期属于排除日期，那么就是按普通的工作日算
        if (excludeHolidays != null) {
            for (Date h : excludeHolidays) {
                String hs = onlyGetDate(h, DATE_FORMAT_DAY);
                if (currday.equals(hs)) {
                    return false;
                }
            }//end for
        }

        // 如果指定日期属于节假日，就按节假日算
        if (holidays != null) {
            for (Date h : holidays) {
                String hs = onlyGetDate(h, DATE_FORMAT_DAY);
                if (currday.equals(hs)) {
                    return true;
                }
            }//end for
        }

        // 如果指定日期属于周末，就按节假日算
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        return day == Calendar.SATURDAY || day == Calendar.SUNDAY;
    }

    /**
     * 把一个字符串代表的假日日期变成日期列表<br>
     * 例如20161109, 20161124~20161126 --> 变成List&lt;Date&gt;
     *
     * @param holidayStr
     * @return
     * @throws ParseException
     */
    static List<Date> parseHoliday(String holidayStr)
            throws ParseException {
        if (holidayStr == null || "".equals(holidayStr.trim())) {
            return new ArrayList();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        List<Date> holidays = new ArrayList<Date>();
        String[] arr = holidayStr.split(",");

        for (String periodStr : arr) {
            // 计算一个日期区间
            String[] periodArray = periodStr.trim().split("~");
            if (periodArray.length >= 2) {
                Date startDate = sdf.parse(periodArray[0].trim());
                Date endDate = sdf.parse(periodArray[1].trim());
                // 加入日期区间的每一天
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(startDate);

                while (calendar.getTime().before(endDate)) {
                    holidays.add(calendar.getTime());
                    calendar.add(Calendar.DATE, 1);

                }//end while
                holidays.add(endDate);

            } else if (periodArray.length == 1) {
                holidays.add(sdf.parse(periodArray[0]));
            }

        }//end for
        return holidays;
    }

    /**
     * 把一个字符串转成日期对象
     *
     * @param text
     * @param datePattern
     * @return
     * @throws ParseException
     */
    static Date parse(String text, String datePattern) throws ParseException {
        if (text == null || "".equals(text.trim())) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
        return sdf.parse(text);
    }

    final static long ONE_MIN = 60 * 1000; // 一分钟
    final static long ONE_HOUR = 60 * 60 * 1000; // 一小时

    /**
     * 把一个毫秒值变成可以读的时分秒
     *
     * @param millisec
     * @return
     */
    static String toHumanReadable(long millisec) {
        long hour = millisec / ONE_HOUR;
        long min = (millisec % ONE_HOUR) / ONE_MIN;
        long sec = ((millisec % ONE_HOUR) % ONE_MIN) / 1000;
        return hour + ":" + appendZero(min) + ":" + appendZero(sec);
    }

    static String appendZero(long num) {
        if (num == 0) {
            return "00";
        }
        if (num < 10) {
            return "0" + num;
        }
        return "" + num;
    }

    public static void main(String[] args) throws ParseException {
//        Date day = DateUtil.convert("2016/9/27 8:15:14",  "yyyy/MM/dd HH:mm:ss");
//        String day = DateUtil.onlyGetDate(new Date(), "yyyyMMdd");
//        Date day = DateUtil.onlyGetDate(new Date());
//        Date day = DateUtil.getCurrentDayFrom10Clock(new Date());
//        Date day = DateUtil.getCurrentDayFrom18Clock(new Date());
//        List<Date> list = DateUtil.parseHoliday("20161001 ~ 20161008, 20161012");
        System.out.println("day=" + DateUtil.toHumanReadable(ONE_HOUR + 61000));
    }
}
